package JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * 使用预编译SQL执行UPDATE语句
 */
public class JDBCDemo9 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()){
            /*
                JDBC中事务是默认自动提交的
                如果我们需要自行控制事务，则要将自动提交事务关闭
             */
            connection.setAutoCommit(false);

            String sql = "UPDATE user " +
                    "SET password = ? , nickname = ? , age = ? " +
                    "WHERE id = ?";

            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,"666888");
            ps.setString(2,"小三");
            ps.setInt(3,88);
            ps.setInt(4,1);
            int num = ps.executeUpdate();
            System.out.println(num>0?"修改成功":"修改失败");

            //connection.commit();
            //System.out.println("提交事务");
            connection.rollback();
            System.out.println("回滚事务");
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
